//
//  LXEditAddressViewCell.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/8/16.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXEditAddressViewCell.h"

@interface LXEditAddressViewCell()

/**删除*/
@property(nonatomic,weak)UIButton *deleteButton;
/**编辑*/
@property(nonatomic,weak)UIButton *editButton;
/**分割线*/
@property(nonatomic,weak)UIView *cutLine;



@end

@implementation LXEditAddressViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.cellHeight = MineRatio(144);
        
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        UILabel *phoneLabel = [[UILabel alloc] init];
        phoneLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:phoneLabel];
        self.phoneLabel = phoneLabel;
        
        UILabel *addressLabel = [[UILabel alloc] init];
        addressLabel.font = [UIFont systemFontOfSize:12];
        addressLabel.numberOfLines = 0;
        [self addSubview:addressLabel];
        self.addressLabel = addressLabel;
        
        UIView *cutLine = [[UIView alloc] init];
        cutLine.backgroundColor = HexColor(@"#EEEEEE");
        [self addSubview:cutLine];
        self.cutLine = cutLine;
        
        UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [deleteButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [deleteButton setBackgroundColor:RGBColor(236, 236, 236)];
        deleteButton.titleLabel.font = [UIFont systemFontOfSize:13];
        deleteButton.layer.cornerRadius = 5;
        [deleteButton addTarget:self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:deleteButton];
        self.deleteButton = deleteButton;
        
        UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [editButton setTitle:@"编辑" forState:UIControlStateNormal];
        [editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [editButton setBackgroundColor:HexColor(@"#3CB034")];
        editButton.titleLabel.font = [UIFont systemFontOfSize:13];
        editButton.layer.cornerRadius = 5;
        [editButton addTarget:self action:@selector(editButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:editButton];
        self.editButton = editButton;
        
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(globalMargin, 0, ScreenWidth-2*globalMargin, self.cellHeight)];
        [self.contentView insertSubview:imageView atIndex:0];
        self.backgroundImageView = imageView;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //姓名
    self.nameLabel.width = [self.nameLabel.text sizeInAre:MaxSize font:self.nameLabel.font].width;
    self.nameLabel.height = [self.nameLabel.text sizeInAre:MaxSize font:self.nameLabel.font].height;
    self.nameLabel.x = 2*globalMargin;
    self.nameLabel.y = 10;
    
    //电话
    self.phoneLabel.width = [self.phoneLabel.text sizeInAre:MaxSize font:self.phoneLabel.font].width;
    self.phoneLabel.height = [self.phoneLabel.text sizeInAre:MaxSize font:self.phoneLabel.font].height;
    self.phoneLabel.x = self.nameLabel.right + MineRatio(10);
    self.phoneLabel.y = self.nameLabel.y;
    
    //收货地址
    self.addressLabel.width = self.width - self.nameLabel.x * 2;
    self.addressLabel.height = ([self.addressLabel.text sizeInAre:MaxSize font:self.addressLabel.font].height) * 2;
    self.addressLabel.x = self.nameLabel.x;
    self.addressLabel.y = self.nameLabel.botton;
    
    //分割线
    self.cutLine.x = self.nameLabel.x;
    self.cutLine.y = self.addressLabel.botton + 18;
    self.cutLine.height = 1;
    self.cutLine.width = self.width - self.cutLine.x * 2;
    
    //编辑按钮
    self.editButton.width = MineRatio(80);
    self.editButton.height = MineRatio(38);
    self.editButton.x = self.width - self.editButton.width - 15;
    self.editButton.y = (self.height - self.cutLine.botton - self.editButton.height) / 2 + self.cutLine.botton;
    
    //删除按钮
    self.deleteButton.width = self.editButton.width;
    self.deleteButton.height = self.editButton.height;
    self.deleteButton.y = self.editButton.y;
    self.deleteButton.x = self.editButton.left - self.deleteButton.width - 10;
}

#pragma mark - 编辑按钮点击
- (void)editButtonClick:(UIButton*)button
{
    if(self.delegate&&[self.delegate respondsToSelector:@selector(LXEditAddressViewCell:didClickEditButton:)]){
        [self.delegate LXEditAddressViewCell:self didClickEditButton:button];
    }
}


-(void)setBackgroundImage:(UIImage *)backgroundImage{
    _backgroundImage = backgroundImage;
    self.backgroundImageView.image = _backgroundImage;
}

-(void)setEnableUnderLine:(BOOL)enableUnderLine{
    _enableUnderLine = enableUnderLine;
    self.cutLine.hidden = !_enableUnderLine;
}

#pragma mark - 删除按钮点击
- (void)deleteButtonClick:(UIButton*)button
{
    if(self.delegate&&[self.delegate respondsToSelector:@selector(LXEditAddressViewCell:didClickDeleteButton:)]){
        [self.delegate LXEditAddressViewCell:self didClickDeleteButton:button];
    }
}

//...................................................................//
//............................我是分割线..............................//
//..................................................................//

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    
}

@end
